package medium;

import pojo.ListNode;

/**
 * @author shizhun
 * @description:
 * @date 2025/9/9 14:49
 * @see DeviceEntity
 */
public class T24 {
    public static void main(String[] args) {
        ListNode listNode = new ListNode(1);
        listNode.next = new ListNode(2);
        listNode.next.next = new ListNode(3);
        listNode.next.next.next = new ListNode(4);
        System.out.println(new T24().swapPairs(listNode));
    }
    public ListNode swapPairs(ListNode head) {
        ListNode node = head;
        if (head == null || head.next == null){
            return head;
        }

        for (int i = 0; ; i++) {
            if (i == 0) {
                node = change(head, head.next, null);
            } else {
                if (head.next == null || head.next.next == null){
                    return node;
                }
                change(head.next, head.next.next, head);
                head = head.next.next;
            }
        }

    }

    public ListNode change(ListNode node1, ListNode node2, ListNode last) {
        node1.next = node2.next;
        node2.next = node1;
        if (last != null) {
            last.next = node2;
        }
        return node2;
    }
}
